#!/bin/bash - 
#===============================================================================
#
#          FILE: java_iconv.sh
# 
#         USAGE: ./java_iconv.sh 
# 
#   DESCRIPTION: 
# 
#       OPTIONS: ---
#  REQUIREMENTS: ---
#          BUGS: ---
#         NOTES: ---
#        AUTHOR: YOUR NAME (), 
#  ORGANIZATION: 
#       CREATED: 2012年06月22日 00时54分23秒 CST
#      REVISION:  ---
#===============================================================================

#set -o nounset                              # Treat unset variables as an error
shopt -s -o nounset
jdk1_8="/usr/lib/jvm/java-1.8.0-openjdk-amd64"
jdk_11="/media/linuxdata/data/opt/java/jdk-11"
#Frdisk=/media/linux/data/weblogic/
Frdisk=/tmp/win
get_char()
{
	SAVEDSTTY=`stty -g`
	stty -echo
	stty raw
	dd if=/dev/tty bs=1 count=1 2> /dev/null
	stty -raw
	stty echo
	stty $SAVEDSTTY
}
#
#从键盘接收yes or no
#返回：0-yes 1-no
#
yesno()
{

	echo -e "【1】: java-1.8.0-openjdk-amd64    ="$jdk1_8
	echo -e "【2】: jdk_11  ="$jdk_11
	echo -n "【q】: 退出"
	echo -n " > "
	while true
	do  
		cmd=`get_char`
		case $cmd in
			1)
				echo $cmd
				return 1
				;;  
			2)
				echo $cmd
				return 2
				;;  
			3)
				echo $cmd
				return 3
				;;  
			4)
				echo $cmd
				return 4
				;;  
			5)
				echo $cmd
				return 5
				;;  
			6)
				echo $cmd
				return 6
				;;  
			q)
				echo $cmd
				return 99
				;;  
			*)  
				continue
				;;  
		esac
	done
}



setJdk8()
{
	JAVA_HOME=$jdk1_8
	PATH=$JAVA_HOME/bin:$PATHOLD
	CLASSPATH=
	#$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar:$JAVA_HOME/jre/lib/rt.jar:/home/fedora/.vim/:
	CLASSPATH=$CLASSPATH:$JAVA_HOME/jre/lib/resources.jar
	CLASSPATH=$CLASSPATH:$JAVA_HOME/jre/lib/rt.jar
	CLASSPATH=$CLASSPATH:$JAVA_HOME/jre/lib/jsse.jar
	CLASSPATH=$CLASSPATH:$JAVA_HOME/jre/lib/jce.jar
	CLASSPATH=$CLASSPATH:$JAVA_HOME/jre/lib/charsets.jar
	CLASSPATH=$CLASSPATH:$JAVA_HOME/jre/lib/jfr.jar
	CLASSPATH=$CLASSPATH:$JAVA_HOME/jre/lib/ext/localedata.jar
	CLASSPATH=$CLASSPATH:$JAVA_HOME/jre/lib/ext/sunec.jar
	CLASSPATH=$CLASSPATH:$JAVA_HOME/jre/lib/ext/dnsns.jar
	CLASSPATH=$CLASSPATH:$JAVA_HOME/jre/lib/ext/sunjce_provider.jar
	CLASSPATH=$CLASSPATH:$JAVA_HOME/jre/lib/ext/sunpkcs11.jar
	CLASSPATH=$CLASSPATH:$JAVA_HOME/jre/lib/ext/zipfs.jar
	CLASSPATH=$CLASSPATH:$JAVA_HOME/lib/tools.jar
	CLASSPATH=.:$CLASSPATH:
	export JAVA_HOME PATH CLASSPATH
	echo "JAVA_HOME="$JAVA_HOME
	echo "CLASSPATH="$CLASSPATH
	echo "PATH="$PATH

}


setJdk_11()
{
	JAVA_HOME=$jdk_11
	PATH=$JAVA_HOME/bin:$PATHOLD
	CLASSPATH=
	CLASSPATH=.:$CLASSPATH:
	export JAVA_HOME PATH CLASSPATH
	echo "JAVA_HOME="$JAVA_HOME
	echo "CLASSPATH="$CLASSPATH
	echo "PATH="$PATH

}


#应用示例
yesno
#先將返回值存放到變量,因爲直接 echo $? 輸出第二次就會失效
inNum=$?
printf "\033[1;40;32m 選擇 $inNum\033[0m\n"
if [ $inNum -eq 1 ];then
	echo "選擇 1"
	setJdk8
elif [ $inNum -eq 2 ];then
	echo "選擇 2"
	setJdk_11
elif [ $inNum -eq 99 ];then
	echo "選擇取消退出"
fi
java -version
set +o nounset                              # 不重新啟用將會出現  !ref: 为绑定变量 錯誤提示
